package com.zisu.rabbitconsumer.component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.rabbitmq.client.Channel;
import com.zisu.rabbitconsumer.Entity.User;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.Message;
import org.springframework.stereotype.Component;

@Component
public class RabbitReceiveES2 {


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="eq3_request_queue",durable = "true"),
                    exchange = @Exchange(name = "es2_exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "eq3_request"
            )
    )
    @RabbitHandler
    public void es2ExchangeEq3(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============es2-eq3_request_queue=================");
        System.out.println("端口:8004 消费体："+message.getPayload());
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        Thread.sleep(5000);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);


        channel.basicAck(deliveryTag,false);

        //对于消费失败的数据可以重回队列
        //channel.basicNack(deliveryTag,false,true);
    }




    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="eq3_response_queue",durable = "true"),
                    exchange = @Exchange(name = "es2_exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "eq3_response"
            )
    )
    @RabbitHandler
    public void es2ExchangeResponseEq3(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============es2-eq3_response_queue=================");
        System.out.println("端口:8004 消费体："+message.getPayload());
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag,false);
    }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="eq4_request_queue",durable = "true"),
                    exchange = @Exchange(name = "es2_exchange",durable = "true",type = "direct",ignoreDeclarationExceptions = "true"),
                    key = "eq4_request"
            )
    )
    @RabbitHandler
    public void es2ExchangeEq4(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============es2-eq4_request_queue=================");
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        byte[] bytes =(byte[])message.getPayload();
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(bytes, User.class);

        Thread.sleep(5000);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        channel.basicAck(deliveryTag,false);

        //对于消费失败的数据可以重回队列
        //channel.basicNack(deliveryTag,false,true);
    }
    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="eq4_response_queue",durable = "true"),
                    exchange = @Exchange(name = "es2_exchange",durable = "true",type = ExchangeTypes.DIRECT,ignoreDeclarationExceptions = "true"),
                    key = "eq4_response"
            )
    )
    @RabbitHandler
    public void es2ExchangeResponseEq4(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============es2-eq4_response_queue=================");
        System.out.println("端口:8004 消费体："+message.getPayload());
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);
        channel.basicAck(deliveryTag,false);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="remote_command_request_queue",durable = "true"),
                    exchange = @Exchange(name = "rc_exchange",durable = "true",type = "topic",ignoreDeclarationExceptions = "true"),
                    key = "RC.#"
            )
    )
    @RabbitHandler
    public void remoteCommand(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============remote_command-rc=================");
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        byte[] bytes =(byte[])message.getPayload();
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(bytes, User.class);

        Thread.sleep(5000);

        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);


        channel.basicAck(deliveryTag,false);

        //对于消费失败的数据可以重回队列
        //channel.basicNack(deliveryTag,false,true);
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="remote_command_response_queue",durable = "true"),
                    exchange = @Exchange(name = "rc_exchange",durable = "true",type = "topic",ignoreDeclarationExceptions = "true"),
                    key = "ACK.#"
            )
    )
    // TOPIC 模式
    //只要有消息过来都会执行这个函数
    @RabbitHandler
    public void remoteCommand2(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============remote_command-ack=================");
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        byte[] bytes =(byte[])message.getPayload();
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(bytes, User.class);
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        try {
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value="remote_command_response_queue",durable = "true"),
                    exchange = @Exchange(name = "rc_exchange",durable = "true",type = "topic",ignoreDeclarationExceptions = "true"),
                    key = "RC.#"
            )
    )
    // TOPIC 模式
    //只要有消息过来都会执行这个函数
    @RabbitHandler
    public void remoteCommandForTest(Message<?> message, Channel channel)throws Exception{
        System.out.println("==============remote_command-Remote=================");
        System.out.println("端口:8004 消费头："+message.getHeaders().get("attr1")+"|"+message.getHeaders().get("attr2"));
        byte[] bytes =(byte[])message.getPayload();
        ObjectMapper objectMapper = new ObjectMapper();
        User user = objectMapper.readValue(bytes, User.class);
        Thread.sleep(5000);
        Long deliveryTag = (Long)message.getHeaders().get(AmqpHeaders.DELIVERY_TAG);

        try {
            channel.basicAck(deliveryTag,false);
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}